package bc

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/bc"
	bcReq "github.com/flipped-aurora/gin-vue-admin/server/model/bc/request"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
	"time"
)

type BcRecordService struct {
}

// CreateBcRecord 创建bcRecord表记录
// Author [piexlmax](https://github.com/piexlmax)
func (bcRecordService *BcRecordService) CreateBcRecord(bcRecord *bc.BcRecord) (err error) {
	err = global.GVA_DB.Create(bcRecord).Error
	return err
}

// DeleteBcRecord 删除bcRecord表记录
// Author [piexlmax](https://github.com/piexlmax)
func (bcRecordService *BcRecordService) DeleteBcRecord(bcRecord bc.BcRecord) (err error) {
	err = global.GVA_DB.Delete(&bcRecord).Error
	return err
}

// DeleteBcRecordByIds 批量删除bcRecord表记录
// Author [piexlmax](https://github.com/piexlmax)
func (bcRecordService *BcRecordService) DeleteBcRecordByIds(ids request.IdsReq) (err error) {
	err = global.GVA_DB.Delete(&[]bc.BcRecord{}, "id in ?", ids.Ids).Error
	return err
}

// UpdateBcRecord 更新bcRecord表记录
// Author [piexlmax](https://github.com/piexlmax)
func (bcRecordService *BcRecordService) UpdateBcRecord(bcRecord bc.BcRecord) (err error) {
	err = global.GVA_DB.Save(&bcRecord).Error
	return err
}

// GetBcRecord 根据id获取bcRecord表记录
// Author [piexlmax](https://github.com/piexlmax)
func (bcRecordService *BcRecordService) GetBcRecord(id uint) (bcRecord bc.BcRecord, err error) {
	err = global.GVA_DB.Where("id = ?", id).First(&bcRecord).Error
	return
}

func (bcRecordService *BcRecordService) GetBcRecordByList(ids request.IdsReq) (list []bc.BcRecord, err error) {
	var bcRecords []bc.BcRecord
	err = global.GVA_DB.Where("id in ?", ids.Ids).Find(&bcRecords).Error
	return bcRecords, err
}

func (bcRecordService *BcRecordService) GetBcRecordByStatus(id int) (list bc.BcRecord, err error) {
	var bcRecords bc.BcRecord
	err = global.GVA_DB.Where("project_id= ? and status =0 ", id).First(&bcRecords).Error
	return bcRecords, err
}

// GetBcRecordByPhone 根据id获取bcRecord表记录
// Author [piexlmax](https://github.com/piexlmax)
func (bcRecordService *BcRecordService) GetBcRecordByPhone(projectId int, phone string) (bcRecord bc.BcRecord, err error) {
	err = global.GVA_DB.Where("project_id = ? and phone_number = ?", projectId, phone).Order("created_at desc").First(&bcRecord).Error
	return
}

// GetBcRecordByPhone 根据id获取bcRecord表记录
// Author [piexlmax](https://github.com/piexlmax)
func (bcRecordService *BcRecordService) GetBcRecordStatusByPhone(projectId int, phone string) (bcRecord bc.BcRecord, err error) {
	err = global.GVA_DB.Where("project_id = ? and phone_number = ? and status =0", projectId, phone).Order("created_at desc").First(&bcRecord).Error
	return
}

// GetBcRecordInfoList 分页获取bcRecord表记录
// Author [piexlmax](https://github.com/piexlmax)
func (bcRecordService *BcRecordService) GetBcRecordInfoList(info bcReq.BcRecordSearch, names []string) (list []bc.BcRecord, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&bc.BcRecord{})
	var bcRecords []bc.BcRecord
	// 如果有条件搜索 下方会自动创建搜索语句
	if info.StartCreatedAt != nil && info.EndCreatedAt != nil {
		db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
	}
	if info.SuperiorsName != "" {
		db = db.Where("superiors_name = ?", info.SuperiorsName)
	}
	if info.SysUserName != "" {
		db = db.Where("sys_user_name = ?", info.SysUserName)
	}
	if info.ProjectName != "" {
		db = db.Where("project_name = ?", info.ProjectName)
	}

	if info.ProjectId > 0 {
		db = db.Where("project_id = ?", info.ProjectId)
	}
	if info.Status >= 0 {
		db = db.Where("status = ?", info.Status)
	}
	if info.PhoneNumber != "" {
		db = db.Where("phone_number = ?", info.PhoneNumber)
	}
	if len(names) > 0 {
		db = db.Where("sys_user_name in ?", names)
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}

	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}

	err = db.Find(&bcRecords).Error
	return bcRecords, total, err
}

func (bcRecordService *BcRecordService) GetBcRecordExpireList() (list []bc.BcRecord, err error) {
	// 创建db
	db := global.GVA_DB.Model(&bc.BcRecord{})
	var bcRecords []bc.BcRecord
	// 如果有条件搜索 下方会自动创建搜索语句
	db = db.Where("created_at < ? ", time.Now().AddDate(0, 0, -7))
	err = db.Find(&bcRecords).Error
	return bcRecords, err
}
